home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS01.ADF / C / raw.c < prev    next >
C/C++ Source or Header  |  1986-01-09  |  9KB  |  315 lines

  1.  
  2. /*************************************************************************/
  3. /*                                                                       */
  4. /*                                Window                                 */
  5. /*                            Steve Ahlstrom                             */
  6. /*                                                                       */
  7. /*************************************************************************/
  8.  
  9. #include "exec/types.h"
  10. #include "intuition/intuition.h"
  11. #include "libraries/dos.h"
  12.  
  13. struct IntuitionBase *IntuitionBase;
  14. struct GfxBase       *GfxBase;
  15. struct Window        *Window;
  16. struct RastPort      *myrastport;
  17.  
  18. int    oldsize;
  19.  
  20. SHORT  row = 8, column = 0;       /* pixel row and column values */
  21. SHORT  ctr;
  22. char   string[2];                 /* stores one character for output */
  23. char   line[80];                  /* stores line of text for wrap */
  24. char   chr;
  25.  
  26. #define INTUITION_REV  30
  27. #define GRAPHICS_REV   30
  28.  
  29. #define COLUMN_SIZE     8
  30.  
  31. struct TextAttr MyFont =
  32. {
  33.      "topaz.font",       /* Font Name   */
  34.      TOPAZ_EIGHTY,       /* Font Height */
  35.      FS_NORMAL,          /* Style       */
  36.      FPF_ROMFONT,        /* Preferences */
  37. };
  38.  
  39. int
  40. Open_Window()
  41. {
  42.      struct NewWindow NewWindow;
  43.  
  44.      /* Open the Intuition library,  The result returned by this call is
  45.       * used to connect your program to the actual Intuition routines
  46.       * in ROM.  If the result of this call is equal to zero, something
  47.       * is wrong and the Intuition you requested is not available, so
  48.       * your program should exit immediately.
  49.       */
  50.  
  51.      IntuitionBase = (struct IntuitionBase *)
  52.                       OpenLibrary("intuition.library", INTUITION_REV);
  53.      if(IntuitionBase == NULL)
  54.           return(FALSE);
  55.  
  56.      GfxBase = (struct GfxBase *)
  57.                 OpenLibrary("graphics.library", GRAPHICS_REV);
  58.      if(GfxBase == NULL)
  59.           return(FALSE);
  60.  
  61.      NewWindow.LeftEdge       =   0;
  62.      NewWindow.TopEdge        =   0;
  63.      NewWindow.Width          = 320;
  64.      NewWindow.Height         = 100;
  65.      NewWindow.DetailPen      =  -1;
  66.      NewWindow.BlockPen       =  -1;
  67.      NewWindow.Title          = "Generic Window Handler";
  68.      NewWindow.Flags          = WINDOWCLOSE | SMART_REFRESH | ACTIVATE |
  69.                                 WINDOWSIZING | WINDOWDRAG | WINDOWDEPTH |
  70.                                 GIMMEZEROZERO;
  71.      NewWindow.IDCMPFlags     = CLOSEWINDOW | RAWKEY | REFRESHWINDOW |
  72.                                 NEWSIZE;
  73.      NewWindow.Type           = WBENCHSCREEN;
  74.      NewWindow.FirstGadget    = NULL;
  75.      NewWindow.CheckMark      = NULL;
  76.      NewWindow.Screen         = NULL;
  77.      NewWindow.BitMap         = NULL;
  78.      NewWindow.MinWidth       = 320;
  79.      NewWindow.MinHeight      = 100;
  80.      NewWindow.MaxWidth       = 640;
  81.      NewWindow.MaxHeight      = 200;
  82.  
  83.      if((Window = (struct Window *) OpenWindow(&NewWindow)) == NULL)
  84.           return(FALSE);
  85.  
  86.      myrastport = Window->RPort;
  87.  
  88.      return(TRUE);
  89. } /* Open_Window */
  90.  
  91. void
  92. Close_Window()
  93. {
  94.      CloseWindow(Window);
  95. } /* Close_Window */
  96.  
  97.  
  98. void
  99. Next_Line_Down()
  100. {
  101.      SHORT i;
  102.  
  103.      if(row >= Window->GZZHeight-8) {
  104.           for (i=0; i<8; i++) {
  105.                ScrollRaster(myrastport, 0, 1, 0, 0,Window->GZZWidth,Window->GZZHeight);
  106.                WaitTOF();
  107.           }
  108.      }
  109.      else
  110.           row += 8;
  111.  
  112. } /* Next_Line_Down */
  113.  
  114.  
  115. void
  116. Cursor_Toggle()           /* dlm  10/23/85 */
  117. /* output a full character block cursor */
  118. {
  119.      SetAPen(myrastport, 0);  /* color register 0 */
  120.      SetDrMd(myrastport, JAM2 | COMPLEMENT);
  121.      Move(myrastport, column, row);
  122.      Text(myrastport, " ", 1);
  123.      SetAPen(myrastport, 1);  /* color register 1 */
  124.      SetDrMd(myrastport, JAM2);
  125. } /* Cursor_Toggle */
  126.  
  127.  
  128. char
  129. raw_to_ascii(keycode, qualifier)
  130. USHORT keycode, qualifier;
  131. /* translate the raw keycode to an ASCII value */
  132. {
  133.      static char translate_table[66] =
  134.           { '`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\\',  0,  '0',
  135.             'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '[', ']',  0,   '1', '2', '3',
  136.             'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ';', 39,   0,   0,   '4', '5', '6',
  137.              0,  'Z', 'X', 'C', 'V', 'B', 'N', 'M', ',', '.', '/',  0,  '.',   '7', '8', '9',
  138.             ' ',  8
  139.           };
  140.      static char sym_translate_table[16] = 
  141.           { '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '|', 0, 0 };
  142.      char ascii_char;
  143.  
  144.      if (keycode < 66) {
  145.           if(keycode < 15 && (qualifier & 0x0007))
  146.                ascii_char = sym_translate_table[keycode];
  147.           else {
  148.                ascii_char = translate_table[keycode];
  149.                if (!(qualifier & 0x0007))        /* not a shifted letter */
  150.                     if(ascii_char >= 'A' && ascii_char <= 'Z')
  151.                          ascii_char |= 0x0020;   /* convert to lower case */
  152.           }
  153.           if(ascii_char == 8)
  154.           return(ascii_char);
  155.      }
  156.      else
  157.           return('\n');
  158.  
  159. } /* raw_to_ascii */
  160.  
  161.  
  162. char
  163. Read_Key_Char(code, qualifier)
  164. USHORT code, qualifier;
  165. {
  166.      /* ignore the shift and caps lock keys */
  167.      if (code < 0x60 || code > 0x62) {
  168.           chr = raw_to_ascii(code, qualifier);
  169.           return(chr);
  170.      }
  171.      else
  172.           return('\0');
  173.  
  174. } /* Read_Key_Char */
  175.  
  176.  
  177. void
  178. Write_Window_Char(chr)
  179. char  chr;
  180. {
  181.      extern void Wrap_It();
  182.  
  183.      Cursor_Toggle();
  184.      
  185.      if(chr == 8) {
  186.           if(column) {
  187.                column -=8;
  188.                Move(myrastport, column, row);
  189.                SetDrMd(myrastport, JAM2);
  190.                Text(myrastport, " ", 1);
  191.                Move(myrastport, column, row);
  192.                ctr--;
  193.                SetDrMd(myrastport, JAM1);
  194.           }
  195.           Cursor_Toggle();
  196.           return;
  197.      }
  198.  
  199.      string[0] = chr;
  200.  
  201.      Move(myrastport, column, row);
  202.      if (string[0] != '\n') {
  203.           Text(myrastport, string, 1);
  204.           line[ctr++] = string[0];
  205.           column += COLUMN_SIZE;
  206.           if(column >= Window->GZZWidth-8)
  207.                Wrap_It();
  208.      }
  209.      else {
  210.           ctr = column = 0;
  211.           Next_Line_Down();
  212.      }
  213.      Cursor_Toggle();
  214.  
  215. } /* Write_Window_Char */
  216.  
  217.  
  218. void
  219. Wrap_It()
  220. {
  221.      SHORT where, i;
  222.  
  223.      for(where = ctr; line[where] != ' ' && where > 0; where--);
  224.           if(ctr - where <= (ctr/2) + 1) {
  225.                Move(myrastport, where*COLUMN_SIZE, row);
  226.                for(i=where; i <= ctr+1; i++)
  227.                     Text(myrastport, " ", 1);
  228.                where++;
  229.                Next_Line_Down();
  230.                column = 0;
  231.                Move(myrastport, column, row);
  232.                for(i=where; i < ctr; i++)
  233.                     line[column++] = line[i];
  234.                Text(myrastport, line, column);
  235.                ctr = column;
  236.                column *= COLUMN_SIZE; /* convert columns to pixels */
  237.           }
  238.           else {
  239.                ctr = column = 0;
  240.                Next_Line_Down();
  241.           }
  242. } /* Wrap_It */
  243.  
  244.  
  245. void
  246. main()
  247. {
  248.      extern struct IntuiMessage *GetMsg();
  249.  
  250.      struct IntuiMessage *message;
  251.  
  252.      ULONG  class;
  253.      USHORT code;
  254.      USHORT qualifier;
  255.      SHORT  done = FALSE;
  256.      int    windowsigbit,
  257.             waitsignal,
  258.             signal;
  259.      
  260.      if (!Open_Window()) {
  261.           printf("Unable to open window\n");
  262.           exit(0);
  263.      }
  264.  
  265.      setmem(line, 80, '\0');
  266.  
  267.      string[1] = '\0';
  268.  
  269.      Cursor_Toggle();
  270.  
  271.      windowsigbit = Window->UserPort->mp_SigBit;
  272.      waitsignal = 1<<windowsigbit;
  273.  
  274.      while(!done) {
  275.           oldsize = Window->GZZHeight;
  276.           signal = Wait(waitsignal);
  277.           if (signal & 1<<windowsigbit) { /* current code */
  278.                while (message = GetMsg(Window->UserPort)) {
  279.                     class     = message->Class;
  280.                     code      = message->Code;
  281.                     qualifier = message->Qualifier;
  282.                     ReplyMsg(message);
  283.                     if(class == NEWSIZE) {
  284.                          if(Window->GZZHeight < oldsize && row > Window->GZZHeight) {
  285.                                  Next_Line_Down(myrastport);
  286.                                  column = 0;
  287.                                  row = (Window->GZZHeight / 8) * 8;
  288.                                  Move(myrastport, column, row);
  289.                                  ClearEOL(myrastport);
  290.                                  Move(myrastport, column, row);
  291.                                  Cursor_Toggle();
  292.                          }
  293.                     }
  294.                     if (class == RAWKEY) {
  295.                          if(code < 0x80) {
  296.                               chr = Read_Key_Char(code, qualifier);
  297.                                    if(chr)
  298.                                    Write_Window_Char(chr);
  299.                          }
  300.                     }
  301.                     else if(class == CLOSEWINDOW)
  302.                          done = TRUE;
  303.  
  304.                     if(class == REFRESHWINDOW) {
  305.                          BeginRefresh(Window);
  306.                          EndRefresh(Window, TRUE);
  307.                     }
  308.                }
  309.           }
  310.      }
  311.      Close_Window();
  312.      exit(0);
  313.  
  314. }  /* main */
  315.